const mongoose = require('mongoose')
mongoose.set('useFindAndModify', false)

const orderSchema = new mongoose.Schema({
    _mealId: { type: Number, required: true }, // 编号
    date: { type: String, required: true }, // 日期
    uTime: { type: String, required: true }, // 更新订单
    orderDetail: { type: Object, required: true }, // 订单详情
    topic: { type: String, required: true }, // kafka主题
    foodType: { type: String, required: true }, // 食品类型
    status: { type: Number, required: true }  //0：未制作 1：已下发制作 2： 制作完成 3： 已取餐 4：下发制作出错 5: 制作出错 6： 取餐超时
})

const HotpotOrderSchema = new mongoose.Schema({
    _mealId: { type: Number, required: true },
    date: { type: String, required: true },
    status: { type: Number, required: true },  //0：未下发 1：已下发 2：下发失败  3：忙碌 5: 成功 6: 成功上报云端
    orderDetail: { type: Object, required: true },
}, { timestamps: { createdAt: 'created', updatedAt: 'updated' } })

const HotpotInfoSchema = new mongoose.Schema({
    storeId: { type: String, required: true },
    callTimer: { type: Number }, //调用叫号
    forceLock: { type: Number }, //1强制停止下发命令 2正常下发
    forceOrder: { type: String }, //下发的命令 "170,6,0,6,0,1,3"
    send: { type: Number }, //1可下发 2不可下发
}, { timestamps: { createdAt: 'created', updatedAt: 'updated' } })

const HotpotInFoodOrderSchema = new mongoose.Schema({
    _inFoodOrderId: { type: Number, required: true },
    date: { type: String, required: true },
    orderDetail: { type: Object, required: true },
    position: { type: Number, required: true }, //1左 2右
    status: { type: Number, required: true }, //0: 未下发 1: 已下发 2: 下发失败 3: 忙碌 4: 格子有餐 5: 成功 6: 成功上报云端
}, { timestamps: { createdAt: 'created', updatedAt: 'updated' } })

const HotpotOutFoodOrderSchema = new mongoose.Schema({
    _outFoodOrderId: { type: Number, required: true },
    date: { type: String, required: true },
    orderDetail: { type: Object, required: true },
    orderDetailId: { type: String },
    orderId: { type: String },
    position: { type: Number, required: true }, //1左 2右
    type: { type: Number, required: true }, // 0取餐 1打烊清库存 2超时清库存
    status: { type: Number, required: true }, //0: 未下发 1: 已下发 2: 下发失败 3: 忙碌 4: 格子无餐 5: 成功 6: 成功上报云端
}, { timestamps: { createdAt: 'created', updatedAt: 'updated' } })

const HotpotPreFurnaceOrderSchema = new mongoose.Schema({
    _preFurnaceOrderId: { type: Number, required: true },
    date: { type: String, required: true },
    quantity: { type: Number, required: true },
    furnaceId: { type: Number, required: true },
    status: { type: Number, required: true }, //0: 未下发 1: 已下发 2: 失败 3: 忙碌 4成功 5:成功上报云端
}, { timestamps: { createdAt: 'created', updatedAt: 'updated' } })

const CounterSchema = new mongoose.Schema({
    _id: { type: String, required: true },
    seq: { type: Number, default: 1 }
});
const counter = mongoose.model('counter', CounterSchema);

HotpotOrderSchema.pre('insertMany', function (next, arr) {
    arr.forEach((val, index) => {
        counter.findByIdAndUpdate({ _id: 'hotpotOrderId' }, { $inc: { seq: 1 } }, function (error, counter) {
            if (error) return next(error);
            val._mealId = counter.seq;
            if (index === arr.length - 1) {
                setTimeout(() => {
                    next()
                }, 100)
            }
        });
    })

});

HotpotInFoodOrderSchema.pre('insertMany', function (next, arr) {
    arr.forEach((val, index) => {
        counter.findByIdAndUpdate({ _id: 'hotpotInFoodOrderId' }, { $inc: { seq: 1 } }, function (error, counter) {
            if (error) return next(error);
            val._inFoodOrderId = counter.seq;
            if (index === arr.length - 1) {
                setTimeout(() => {
                    next()
                }, 100)
            }
        });
    })

});

HotpotOutFoodOrderSchema.pre('insertMany', function (next, arr) {
    arr.forEach((val, index) => {
        counter.findByIdAndUpdate({ _id: 'hotpotOutFoodOrderId' }, { $inc: { seq: 1 } }, function (error, counter) {
            if (error) return next(error);
            val._outFoodOrderId = counter.seq;
            if (index === arr.length - 1) {
                setTimeout(() => {
                    next()
                }, 100)
            }
        });
    })

});

HotpotPreFurnaceOrderSchema.pre('insertMany', function (next, arr) {
    arr.forEach((val, index) => {
        counter.findByIdAndUpdate({ _id: 'hotpotPreFurnaceOrderId' }, { $inc: { seq: 1 } }, function (error, counter) {
            if (error) return next(error);
            val._preFurnaceOrderId = counter.seq;
            if (index === arr.length - 1) {
                setTimeout(() => {
                    next()
                }, 100)
            }
        });
    })

});

const HotpotDeviceStatusSchema = new mongoose.Schema({
    date: { type: String, required: true },
    detail: { type: Object, required: true },
    head: { type: String, required: true },
}, { timestamps: { createdAt: 'created', updatedAt: 'updated' } })

const QrcodesSchema = new mongoose.Schema({
    str: { type: String, require: true },
    flag: { type: Boolean, require: true },
    type: { type: Number, require: true },
}, { timestamps: { createdAt: 'created', updatedAt: 'updated' } })




const order = mongoose.model('order', orderSchema)
const HotpotOrder = mongoose.model('hotpotOrder', HotpotOrderSchema)
const HotpotInFoodOrder = mongoose.model('hotpotInFoodOrder', HotpotInFoodOrderSchema)
const HotpotOutFoodOrder = mongoose.model('hotpotOutFoodOrder', HotpotOutFoodOrderSchema)
const HotpotPreFurnaceOrder = mongoose.model('hotpotPreFurnaceOrder', HotpotPreFurnaceOrderSchema)
const HotpotDeviceStatus = mongoose.model('hotpotDeviceStatus', HotpotDeviceStatusSchema)
const HotpotInfo = mongoose.model('HotpotInfo', HotpotInfoSchema)
const Qrcodes = mongoose.model('Qrcodes', QrcodesSchema)

module.exports = {
    order: order,
    HotpotOrder,
    HotpotInFoodOrder,
    HotpotOutFoodOrder,
    HotpotPreFurnaceOrder,
    HotpotDeviceStatus,
    HotpotInfo,
    counter,
    Qrcodes
}